﻿package com.alert
{
        import flash.display.Sprite;
        import flash.text.TextField;
        import flash.text.TextFormat;
        import flash.display.Stage;
        import com.alert.DataManager;
        import flash.events.MouseEvent;
        import flash.events.Event;
        
        /**
        * @ Package         : Alert
        * @ Author                : Martin Mao
        * @ MSN/Mail        : im@mococo.cn
        * @ QQ                        : 183654371
        * @ Homepage        : www.mococo.cn
        */
        
        public class Alert extends Sprite
        {
                private static var __Instance:Alert;
                private static var alertBg:Sprite;
                private static var alertInfo:TextField;
                
                //皮肤设定
                //背景颜色
                private static var alertBgColor:uint = 0x444444;
                //是否圆角
                private static var alertRadius:Boolean = true;
                //圆角zhishu
                private static var alertRoundRadius:int = 6;
                //标题颜色
                private static var alertTitleColor:String = 'ffffff';
                //消息颜色
                private static var alertFontColor:String = '999999';
                //分割线颜色
                private static var alertTitleLineColor:uint = 0x000000;
                //组件字体
                private static var alertFont:String = 'Verdana';
                //组件字体大小
                private static var alertFontSize:int = 12;
                //组件宽
                private static var alertW:Number = 250;
                //组件高
                private static var alertH:Number = 100;
                
                public function Alert(e:Enforce)
                {
                        if ( e == null )
                        {
                                throw new Error("单例类，无法用new新建，请使用 instance 获取它的唯一实例");
                        }
                        //画一个背景
                        alertBg = new Sprite();
                        alertBg.graphics.beginFill(alertBgColor, 1);
                        if ( Boolean(alertRadius) ) alertBg.graphics.drawRoundRect(0, 0, alertW, alertH, alertRoundRadius);
                        else alertBg.graphics.drawRect(0, 0, alertW, alertH);
                        alertBg.graphics.beginFill(alertTitleLineColor, 1);
                        alertBg.graphics.drawRect(10, 30, Number(alertW) - 20, 2);
                        alertBg.graphics.endFill();
                        addChild(alertBg);
                        
                        //标题文本框和消息文本框
                        alertInfo = new TextField();
                        alertInfo.width = Number(alertW) - 20;
                        alertInfo.height = Number(alertH) - 20;
                        alertInfo.x = alertInfo.y = 10;
                        alertInfo.mouseEnabled = false;
                        var alertFMT:TextFormat = new TextFormat(alertFont, Number(alertFontSize));// , alertFontColor);
                        alertInfo.defaultTextFormat = alertFMT;
                        alertInfo.multiline = true;
                        alertFMT = null;
                        addChild(alertInfo);
                        //坐标处于舞台正中央
                        
                        x = ( DataManager.STAGE_WIDTH - Number(alertW) ) / 2;
                        y = ( DataManager.STAGE_HEIGHT - Number(alertH) ) / 2;
                        
                        //添加关闭侦听，初始时设置为不可见
                        this.addEventListener(MouseEvent.CLICK, clostThis);
                        this.buttonMode = true;
                        this.visible = false;
                }
                
                //单例模式构造
                public static function get Instance():Alert
                {
                        if ( Alert.__Instance == null )
                        {
                                Alert.__Instance = new Alert(new Enforce());
                        }
                        return Alert.__Instance;
                }
                
                //添加到舞台
                public static function show(s:*, t:String = '提示'):void
                {
                        try
                        {
                                //先把舞台上所有的可见元素设置为不接受鼠标事件
                                for ( var i:int = 0; i < DataManager.STAGE_ROOT.numChildren; i ++ ) DataManager.STAGE_ROOT.getChildAt(i).mouseEnabled = false;
                                //将Alert实例添加到舞台最上面
                                DataManager.STAGE_ROOT.addChild(Alert.Instance);
                                //显示消息
                                alertInfo.htmlText = "<b><font color=\'#" + alertTitleColor + "\'>" + t + " (点击直接关闭)</font></b><br><br><br><span style='text-align:center'><font color=\'#" + alertFontColor + "\'>&nbsp;&nbsp;&nbsp;&nbsp;" + String(s) + "</font></span>";
                                //设置Alert实例可见
                                Alert.__Instance.visible = true;
                        }
                        catch (e:Error)
                        {};
                }
				
				public static function msg(s:*,t:Boolean=false):void{
					try{
						if(!t){
						//画一个背景
                        alertBg = new Sprite();
                        alertBg.graphics.beginFill(alertBgColor, 1);
                        alertBg.graphics.drawRoundRect(0, 0, alertW, 50, alertRoundRadius);
               			
						alertBg.x = ( DataManager.STAGE_WIDTH - Number(alertW) ) / 2;
                        alertBg.y = ( DataManager.STAGE_HEIGHT - Number(50) ) / 2;
                        DataManager.STAGE_ROOT.addChild(alertBg);
						//标题文本框和消息文本框
                        alertInfo = new TextField();
                        alertInfo.width = Number(alertW) - 20;
                        alertInfo.height = 20;
                        alertInfo.x = alertInfo.y = 15;
                        alertInfo.mouseEnabled = false;
                        var alertFMT:TextFormat = new TextFormat(alertFont, Number(alertFontSize));// , alertFontColor);
                        alertInfo.defaultTextFormat = alertFMT;
                        alertInfo.multiline = true;
                        alertFMT = null;
						alertInfo.text = s;
						alertInfo.textColor = 0xffffff;
                        alertBg.addChild(alertInfo);
						//alertBg.removeChild(alertInfo);
						}else{
							//先把舞台上所有的可见元素设置为不接受鼠标事件
                                for ( var i:int = 0; i < DataManager.STAGE_ROOT.numChildren; i ++ ) DataManager.STAGE_ROOT.getChildAt(i).mouseEnabled = false;
                                //将Alert实例添加到舞台最上面
                                DataManager.STAGE_ROOT.addChild(Alert.Instance);
                                //显示消息
                                alertInfo.htmlText = "<b><font color=\'#" + alertTitleColor + "\'>" + t + " (点击直接关闭)</font></b><br><br><br><span style='text-align:center'><font color=\'#" + alertFontColor + "\'>&nbsp;&nbsp;&nbsp;&nbsp;" + String(s) + "</font></span>";
                                //设置Alert实例可见
                                Alert.__Instance.visible = true;
						}
					}catch(e:Error){
						
					}
				}
                public static function msgClose():void{
					try{
					 DataManager.STAGE_ROOT.removeChild(alertBg);
					}catch(e:Error){}
				}
                private function clostThis(e:MouseEvent):void
                {
                        Alert.__Instance.visible = false;
                        //把舞台上所有的可见元素还原接受鼠标事件
                        for ( var i:int = 0; i < DataManager.STAGE_ROOT.numChildren; i ++ ) DataManager.STAGE_ROOT.getChildAt(i).mouseEnabled = true;
                }
        }
        
}

        class Enforce{}